using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using DevComponents.DotNetBar;

namespace Demo
{
    internal static class TraditionalChineseHelper
    {
        internal static string Convert(string? text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return text ?? string.Empty;
            }

            try
            {
                return Strings.StrConv(text, VbStrConv.TraditionalChinese, 0);
            }
            catch
            {
                return text;
            }
        }

        internal static void Apply(Control control)
        {
            if (control == null)
            {
                return;
            }

            control.Text = Convert(control.Text);

            if (control is ToolStrip toolStrip)
            {
                Apply(toolStrip);
            }

            foreach (Control child in control.Controls)
            {
                Apply(child);
            }
        }

        private static void Apply(ToolStrip toolStrip)
        {
            foreach (ToolStripItem item in toolStrip.Items)
            {
                Apply(item);
            }
        }

        private static void Apply(ToolStripItem item)
        {
            item.Text = Convert(item.Text);

            if (item is ToolStripDropDownItem dropdownItem)
            {
                foreach (ToolStripItem subItem in dropdownItem.DropDownItems)
                {
                    Apply(subItem);
                }
            }
        }

        internal static void Apply(Bar? bar)
        {
            if (bar == null)
            {
                return;
            }

            bar.Text = Convert(bar.Text);
            foreach (BaseItem item in bar.Items)
            {
                Apply(item);
            }
        }

        private static void Apply(BaseItem item)
        {
            item.Text = Convert(item.Text);
            foreach (BaseItem subItem in item.SubItems)
            {
                Apply(subItem);
            }
        }
    }
}

